package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.netty.util.internal.EmptyPriorityQueue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.service.Tags;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@Api(tags = "员工类相关接口")
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        // 登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        // 直接调用工具类的generateJwt方法（秘钥和过期时间已在工具类内部固定）
        String token = JwtUtil.generateJwt(claims);

        // 组装返回结果（假设EmployeeLoginVO包含token和员工基本信息）
        EmployeeLoginVO loginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(loginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("员工登出")
    @PostMapping("/logout")
    public Result<String> logout() {

        return Result.success();
    }

    /**
     * 新增员工
     */
    @ApiOperation("新增员工接口")
    @PostMapping
    public Result<PageResult> addEmployee(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工{}", employeeDTO);
        employeeService.addEmployee(employeeDTO);
        return Result.success();
    }

    /**
     * 员工分页查询
     */
    @ApiOperation("员工分页查询接口")
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO ){
        log.info("获取到的数据是：{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.page(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     *启用员工与禁用员工
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("禁用/启用员工账号接口")
    @PostMapping("/status/{status}")
    public Result staTus(@PathVariable Integer status,Long id){
        log.info("接收到的status是：{},id是：{}",status,id);
        employeeService.startorstop(status,id);
        return Result.success();
    }

    /**
     * 根据id查询员工数据
     * @param id
     * @return
     */
    @ApiOperation("根据员工id查询数据接口")
    @GetMapping("/{id}")
    public Result<Employee> selectById(@PathVariable Integer id){
        log.info("接收到的id是：{}",id);
        Employee emplist = employeeService.selectById(id);
        return Result.success(emplist);
    }

    /**
     * 修改员工信息
     *
     */

    @ApiOperation("修改员工信息接口")
    @PutMapping
    public Result upDateEmp(@RequestBody EmployeeDTO employeeDTO){
        log.info("接收到的修改参数是：{}",employeeDTO);
        employeeService.upDateEmp(employeeDTO);
        return Result.success();
    }

}
